package cn.iocoder.yudao.module.expert.service.expertbasedata;

import cn.iocoder.yudao.module.expert.dal.mysql.expertbasedata.ExpertBaseDataMapper;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import cn.iocoder.yudao.module.expert.controller.admin.expertbasedata.vo.*;
import cn.iocoder.yudao.module.expert.dal.dataobject.expertbasedata.ExpertBaseDataDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import java.util.List;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.expert.enums.ErrorCodeConstants.*;

/**
 * 专业字典 Service 实现类
 *
 * @author 管理员
 */
@Service
@Validated
public class ExpertBaseDataServiceImpl implements ExpertBaseDataService {

    @Resource
    private ExpertBaseDataMapper baseDataMapper;

    @Override
    public Long createBasedata(ExpertBaseDataSaveReqVO createReqVO) {
        // 插入
        ExpertBaseDataDO basedata = BeanUtils.toBean(createReqVO, ExpertBaseDataDO.class);
        baseDataMapper.insert(basedata);
        // 返回
        return basedata.getId();
    }

    @Override
    public void updateBasedata(ExpertBaseDataSaveReqVO updateReqVO) {
        // 校验存在
        validateBasedataExists(updateReqVO.getId());
        // 更新
        ExpertBaseDataDO updateObj = BeanUtils.toBean(updateReqVO, ExpertBaseDataDO.class);
        baseDataMapper.updateById(updateObj);
    }

    @Override
    public void deleteBasedata(Long id) {
        // 校验存在
        validateBasedataExists(id);
        // 删除
        baseDataMapper.deleteById(id);
    }

    private void validateBasedataExists(Long id) {
        if (baseDataMapper.selectById(id) == null) {
            throw exception(BASEDATA_NOT_EXISTS);
        }
    }

    @Override
    public ExpertBaseDataDO getBasedata(Long id) {
        return baseDataMapper.selectById(id);
    }

    @Override
    public PageResult<ExpertBaseDataDO> getBasedataPage(ExpertBaseDataPageReqVO pageReqVO) {
        return baseDataMapper.selectPage(pageReqVO);
    }

    @Override
    public List<ExpertBaseDataDO> getMajorList() {
        return baseDataMapper.selectList();
    }

}